package Day24;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class FuDemo01 {
    private ServerSocket serverSocket;
    private DaoExpress d = new DaoExpress();

    public static void main(String[] args) {
        FuDemo01 server = new FuDemo01();
        server.start();
    }


    public void start(){
        try{
            serverSocket = new ServerSocket(65535);
            System.out.println("服务器启动");
            while(true){
                Socket socket = serverSocket.accept();//等待客户端连接
                System.out.println("有客户连接");
                new Thread(){
                    @Override
                    public void run() {
                        try {
                            receive(socket);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(serverSocket != null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    //创建对象输出流，与客户端进行数据交互
    public void receive(Socket socket) throws IOException {
        InputStream is = socket.getInputStream();
        OutputStream os = socket.getOutputStream();
        ObjectInputStream ois = new ObjectInputStream(is);
        ObjectOutputStream oos = new ObjectOutputStream(os);
        try(is;os;ois;oos){//这种方式可以在try/catch结束之后自动关闭资源
            aa:while (true){
                switch (ois.readInt()){
                    case 1:{
                        gClient(ois,oos);//管理员功能模块
                        break;
                    }
                    case 2:{
                        uClient(ois,oos);//用户功能模块
                        break;
                    }
                    case 0:{
                        break aa;
                    }
                    default: break ;
                }
            }
        }catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }
    }


    //管理员操作界面
    public void gClient(ObjectInputStream ois,ObjectOutputStream oos) throws IOException, ClassNotFoundException {
        while (true){
            switch (ois.readInt()){
                case 1:{
                    add(ois,oos);
                }
                case 2:{
                    delete(ois,oos);
                }
                case 3:{
                    revise(ois,oos);
                }
                case 4:{
                    printAll(oos);
                }
                case 0:{
                    return;
                }
            }
        }
    }

    //添加快递
    public void add(ObjectInputStream ois,ObjectOutputStream oos) throws IOException, ClassNotFoundException {
        Express e = (Express) ois.readObject();
        Express e1 = d.findByNumber(e.getNumber());
        oos.writeObject(e1);
        oos.flush();
        if(e1 == null){
            oos.writeBoolean(d.add(e));
            d.writeToFile();
            oos.flush();
        }
    }

    //删除快递
    public void delete(ObjectInputStream ois,ObjectOutputStream oos) throws IOException, ClassNotFoundException {
        String number = (String) ois.readObject();
        Express e = d.findByNumber(number);
        oos.writeObject(e);
        oos.flush();
        if(e != null){//快递存在
            oos.writeBoolean(d.delete(number));
            d.writeToFile();
            oos.flush();
        }
    }

    //修改快递
    public void revise(ObjectInputStream ois,ObjectOutputStream oos) throws IOException, ClassNotFoundException {
        String s = (String) ois.readObject();
        Express e = d.findByNumber(s);
        oos.writeObject(e);
        oos.flush();
        if(e != null){
            Express e1 = (Express) ois.readObject();
            oos.writeBoolean(d.delete(s));
            oos.writeBoolean(d.revise(e));
            d.writeToFile();
            oos.flush();
            oos.flush();
        }
    }

    public void printAll(ObjectOutputStream oos) throws IOException {
        ArrayList<Express> list = d.printAll();
        oos.writeObject(list);
        oos.flush();
    }

    //用户操作界面
    public void uClient(ObjectInputStream ois,ObjectOutputStream oos) throws IOException {
        int code = ois.readInt();
        Express e = d.findCode(code);
        oos.writeObject(e);
        oos.flush();
        if(e != null){
            oos.writeBoolean(d.delete(e.getNumber()));
            d.writeToFile();
            oos.flush();
        }
    }

}

